package com.compass.mobile.ui.fragment

import UserInfoManager
import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.view.View
import androidx.fragment.app.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentCaseDetailsBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.constant.KEY_CLASSIFY_ID
import com.compass.mobile.framework.constant.KEY_ORDER_ID
import com.compass.mobile.framework.constant.NURSE_ACTION
import com.compass.mobile.framework.constant.YD_USER_ID
import com.compass.mobile.framework.constant.YY_NURSING
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.LiveDataBus
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.Nurse
import com.compass.mobile.presenters.CaseDetailsItemPresenter
import com.compass.mobile.ui.activity.CaseDetailsActivity
import com.compass.mobile.ui.dialog.CodeDialog
import com.compass.mobile.ui.dialog.CustomerServiceCode
import com.compass.mobile.ui.viewModel.NurseProjectViewModel
import com.compass.mobile.widget.MyBridgeAdapter
import setUrlRound

/**
 * Description： 护理详情
 * Date:2024/5/21 21:01
 */
class CaseDetailsFragment : BaseVBFragment<FragmentCaseDetailsBinding>() {
    private val nurseProjectViewModel: NurseProjectViewModel by viewModels()
    private var nurses: MutableList<Nurse> = mutableListOf()
    private var orderId: String? = null
    private var classifyId: String? = null
    private lateinit var oderCodeDialog: CodeDialog.Builder
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            orderId = it.getString(KEY_ORDER_ID)
            classifyId = it.getString(KEY_CLASSIFY_ID)
        }


        FocusViewUtils.scaleView(mBinding?.tvAppointment, 1)

    }

    override fun initData() {
        showLoading()
        nurseProjectViewModel.geNurseDetail(orderId)
        nurseProjectViewModel.geHotNurse()
        nurseProjectViewModel.nurseDetailLiveData.observe(this) {
            it?.let {
                mBinding?.tvCaseName?.text = "护理名称：${it.title}"
                it.imgs?.let { imgLists ->
                    if (imgLists.isNotEmpty()) {
                        mBinding?.imgCase?.setUrlRound(imgLists[0], dpToPx(10))
                    }
                }
                mBinding?.tvPrice?.text = "￥${it.price}元/次"
                mBinding?.tvNumber?.text = "预约人数：${it.reservationNum}"
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    mBinding?.tvContent?.text =

                        Html.fromHtml(it.content, Html.FROM_HTML_MODE_LEGACY)
                } else {
                    mBinding?.tvContent?.text =

                        Html.fromHtml(it.content)
                }

            }
            dismissLoading()
        }
        nurseProjectViewModel.nurseLiveData.observe(this) {
            it?.let { list ->
                setNurseData(list)
            }
        }
        LiveDataBus.get().with(NURSE_ACTION, String::class.java)
            .observe(this) {
                if (::oderCodeDialog.isInitialized) {
                    oderCodeDialog.dismissDialog()
                }
                CustomerServiceCode.Builder(requireActivity(), NURSE_ACTION).show()
            }

    }

    override fun intListener() {
        mBinding?.tvAppointment?.onClick {
            oderCodeDialog = CodeDialog.Builder(
                requireActivity(),
                "${YY_NURSING}${UserInfoManager.getString(YD_USER_ID)}&id=$orderId"
            )
            oderCodeDialog.show()
        }
    }


    private fun setNurseData(nurseList: MutableList<Nurse>) {
        nurses = nurseList
        mBinding?.caseItem?.post {
            // 使用获取到的宽度
            val width = mBinding?.caseItem?.width
            width?.let {
                specialColumn(it)
            }
        }
    }


    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
            val item = nurses[position]
            CaseDetailsActivity.start(requireContext(), item.id, item.classifyId)
        }
    }


    @SuppressLint("RestrictedApi", "SuspiciousIndentation")
    fun specialColumn(width: Int) {
        mBinding?.caseItem?.setNumColumns(4)
        mBinding?.caseItem?.horizontalSpacing = dpToPx(10)
        val finalWidth = width - dpToPx(200)
        val caseDetailsItemPresenter = CaseDetailsItemPresenter()
        caseDetailsItemPresenter.setFinalWidth(finalWidth / 4)
        val arrayObjectAdapter = ArrayObjectAdapter(caseDetailsItemPresenter)
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )
        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, nurses)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_SMALL, false
        )
    }

}
